package com.xiaohudie.JDBC;


import com.mysql.jdbc.Driver;


import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;

/**
 * @Author xiao hu die
 * @Date 2022/3/12 20:38
 * @Version 1.0
 */
public class TestJdbc {
 public static void main(String[] args) {
  /**连接对象先定义出来*/
  Connection connection = null;
  /**定义数据库操作对象*/
  Statement  statement = null;
  /**定义结果集对象*/
  ResultSet resultSet =null;
  try {
   /**注册Driver驱动*/
   DriverManager.registerDriver(new Driver());
   /**获取mysql连接*/
   connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/liu", "root", "123456");
   System.out.println(connection);
   /**获取数据库操作对象*/
   statement = connection.createStatement();
   /**获取sql语句*/
   String insertSql ="INSERT INTO user (id,name,phone) VALUES (6,'chnJsak','13695478526')";
   int i = statement.executeUpdate(insertSql);
   System.out.println("受影响的行数------->"+i);
   String selectSql = "SELECT name , phone FROM user";
   resultSet = statement.executeQuery(selectSql);
   while (resultSet.next()){
    String name = resultSet.getString("name");
    String phone = resultSet.getString("phone");
    System.out.println("name"+"------->"+name+"phone"+"------->"+phone);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   /**先关闭结果集*/
   if (resultSet !=null){
    try {
     resultSet.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   if (statement !=null){
    try {
     statement.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   if (connection!=null){
    try {
     connection.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
 }
}
